package com.A21MobileSales;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.A21.Object.Global;
import com.A21MobileSales.DataBase.DatabaseHelper;
import com.A21MobileSales.JsonServices.Download;

public class SimpleWebviewPopUp extends Activity {

	DatabaseHelper ObjDBHelper;
	String other, title1, title2,function,TrnNo,TrnType;
	Button btn_Home, btn_back;
	TextView tv_Title1,tv_Title2;
	String strHTML = "";
	WebView wv;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.popup_webview);
		try {

			if (getIntent().hasExtra("title1")) {
				title1 = getIntent().getStringExtra("title1");
			}
			if (getIntent().hasExtra("title2")) {
				title2 = getIntent().getStringExtra("title2");
			}
			if (getIntent().hasExtra("function")) {
				function = getIntent().getStringExtra("function");
			}
			if (getIntent().hasExtra("other")) {
				other = getIntent().getStringExtra("other");
			} 
			if (getIntent().hasExtra("TrnType")) {
				TrnType = getIntent().getStringExtra("TrnType");
			} 
			if (getIntent().hasExtra("TrnNo")) {
				TrnNo = getIntent().getStringExtra("TrnNo");
			} 
			wv=(WebView)findViewById(R.id.webview1); 
			wv.getSettings().setBuiltInZoomControls(true);
			tv_Title1 = (TextView) findViewById(R.id.tv_Title1);
			tv_Title2 = (TextView) findViewById(R.id.tv_Title2); 
			tv_Title1.setText(title1); 
			tv_Title2.setText(title2); 
			new SyncTask().execute();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private class  SyncTask extends AsyncTask<String, Void, Void> {

		private ProgressDialog dialog = new ProgressDialog(SimpleWebviewPopUp.this);
		String StrMsg = null;

		protected void onPreExecute() {
			this.dialog.setMessage("Loading...");
			this.dialog.setCanceledOnTouchOutside(false);
			this.dialog.show();
		}

		// automatically done on worker thread (separate from UI thread)
		protected Void doInBackground(final String... a) {
			try {

				String strUserName = Global.getLoggedInUser().User;
				String strPassword = String
						.valueOf(Global.getLoggedInUser().strPassword);
				 
				Download ObjDL = new Download(SimpleWebviewPopUp.this, strUserName,
						strPassword);
				if(function.equals("GetOnPO")){ 
					strHTML = ObjDL.GetOnPO(other);
				}else if(function.equals("GetOnSOQty")){ 
					strHTML = ObjDL.GetOnSOQty(other);
				}else if(function.equals("GetWHQ")){ 
					strHTML = ObjDL.GetWHQ(other);
				}else if(function.equals("TrnDetails")){
					strHTML=ObjDL.GetInvoiceDetails(TrnType, TrnNo); 
				} 
				StrMsg = "Success";
			} catch (Exception ex) {
				StrMsg = ex.getMessage();
			}
			return null;
		}

		// can use UI thread here
		protected void onPostExecute(final Void unused) {

			if (this.dialog.isShowing()) {
				this.dialog.dismiss();
				this.dialog = null;
			}
			try {
				if (StrMsg != null) {
					if (StrMsg.equals("Success")) {
						
						 strHTML = "<link rel=\"stylesheet\" href=\"jquery.mobile-1.0.1.min.css\" /> " +
						 		"<script src=\"jquery-1.6.4.min.js\"></script> " +
						 		"<script src=\"jquery.mobile-1.0.1.min.js\"></script>" + strHTML; 
						 wv.loadDataWithBaseURL("file:///android_asset/", strHTML, "text/html", "UTF-8", null); 
						// wv.loadData(strHTML, "text/html", "UTF-8");
					} else {
						Toast.makeText(SimpleWebviewPopUp.this, StrMsg,
								Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(SimpleWebviewPopUp.this,
							"Please check the network connection!!!",
							Toast.LENGTH_SHORT).show();
				}

			} catch (Exception ex) {

				ex.printStackTrace();

			}

		}

	}

}